package com.lam.mall.service.impl;

import java.util.List;
import java.util.Objects;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lam.common.utils.CollectionUtil;
import com.lam.mall.entity.Area;
import com.lam.mall.entity.TransFeeCity;
import com.lam.mall.entity.TransFeeItem;
import com.lam.mall.mapper.TransFeeCityMapper;
import com.lam.mall.mapper.TransFeeItemMapper;
import com.lam.mall.param.TransFeeItemParam;
import com.lam.mall.service.TransFeeItemService;

/**
 * 运费项Service业务层处理
 * 
 * @author admin
 * @date 2023-04-21
 */
@Service
public class TransFeeItemServiceImpl  implements TransFeeItemService {

    @Autowired
    private TransFeeItemMapper transFeeItemMapper;
    @Autowired
    private TransFeeCityMapper transFeeCityMapper;

    @Override
	public List<TransFeeItem> selectByTemplateId(String templateId) {
    	List<TransFeeItem> list = transFeeItemMapper.selectList(new QueryWrapper<TransFeeItem>().eq("trans_fee_template_id", templateId));
    	if(CollectionUtil.isNotEmpty(list)) {
    		for (TransFeeItem feeItem : list) {
    			feeItem.setCityList(transFeeCityMapper.selectCityList(feeItem.getId()));
			}
    	}
		return list;
	}
    
    /**
     * 查询运费项
     * 
     * @param id ID
     * @return 单个对象
     */
    @Override
    @Transactional(readOnly = true)
    public TransFeeItem selectById(String id) {

        return transFeeItemMapper.selectById(id);
    }

    /**
     * 查询运费项列表
     * 
     * @param param
     * @return 
     */
    @Override
    @Transactional(readOnly = true)
    public List<TransFeeItem> selectList(TransFeeItemParam param) {
    
        QueryWrapper<TransFeeItem> queryWrapper = new QueryWrapper<>();
        
        queryWrapper.eq(Objects.nonNull(param.getTransFeeTemplateId()), "trans_fee_template_id", param.getTransFeeTemplateId());

        return transFeeItemMapper.selectList(queryWrapper);
    }

    /**
     * 新增运费项
     * 
     * @param transFeeItem 
     * @return 
     */
    @Override
    @Transactional
    public int insert(TransFeeItem transFeeItem) {
    
    	transFeeItemMapper.insert(transFeeItem);
    	
		List<Area> cityList = transFeeItem.getCityList();
		if (CollectionUtil.isNotEmpty(cityList)) {
			for (Area area : cityList) {
				TransFeeCity transFeeCity = new TransFeeCity();
				transFeeCity.setTransFeeItemId(transFeeItem.getId());
				transFeeCity.setCityId(area.getId());
				transFeeCityMapper.insert(transFeeCity);
			}
		}
        return 1;
    }

    /**
     * 修改运费项
     * 
     * @param transFeeItem 
     * @return 
     */
    @Override
    @Transactional
    public int update(TransFeeItem transFeeItem) {
    
        return transFeeItemMapper.updateById(transFeeItem);
    }

    /**
     * 删除运费项信息
     * 
     * @param idList ID
     * @return 
     */
    @Override
    @Transactional
    public int deleteById(List<String> idList) {
    
        if(CollectionUtil.isEmpty(idList)) {
            return 0;
        }
        
        transFeeItemMapper.deleteBatchIds(idList);
        
        //删除所有的运费项包含的城市
        QueryWrapper<TransFeeCity> queryWrapper = new QueryWrapper<>();
        if(idList.size() == 1) {
        	queryWrapper.eq("trans_fee_item_id", idList.get(0));
        }else {
        	queryWrapper.in("trans_fee_item_id", idList);
        }
        transFeeCityMapper.delete(queryWrapper);
        
        return 1;
    }

}
